<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script>
			function cal() {
				var num = document.getElementById("num").value;
				var span = document.getElementById("result");
				if (num == "") {
					span.innerHTML = "不能为空";
					return;
				}
				if (isNaN(num)) {
					span.innerHTML = "这不是一个数字";
					return;
				}
				
				if (num < 0) {
					span.innerHTML = "负数没有阶乘";
				} else if (num == 0) {
					span.innerHTML = 1;
				} else {
					// n!=n*(n-1)*...3*2*1
					var result = 1;
					for (var i = num; i >= 1; i--) {
						result = result * i;
					}
					span.innerHTML = result;
				}
			}
			
		</script>
	</head>
	<body>
		<input type="text" id="num" />
		<input  type="button" value="阶乘" onclick="cal()"/>
		=
		<span id="result"></span>
	</body>
</html>
